package com.wxtbk.controller;

import java.util.List;

import com.fanerma.model.TbkProduct;
import com.javen.model.Users;
import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.wxtbk.model.WtCat;
import com.wxtbk.model.WtItem;
import com.wxtbk.model.WtWxuser;
import com.wxtbk.service.TbkService;
import com.wxtbk.utils.HttpRequest;
import com.wxtbk.utils.RegexUtils;

public class FrontController extends Controller{
	
	public void index() {
		String openId = getSessionAttr("openId");
		
		if(openId == null ){
			toOauth();
			return;
		}
		List<WtCat> lstcat=WtCat.dao.findShow();
		setAttr("catlist",lstcat);		
		setAttr("openId", openId);

		int catid = 1;
		try{
			catid = Integer.parseInt(getPara("catid"));
		}catch(Exception ex){}
		setAttr("catid",catid);
		setAttr("keywords",getPara("key"));
		List<WtItem> list =WtItem.dao.GetPromotion(); 
		setAttr("promotions",list);
		

		render("index.jsp");
	}
	
	//跳转到授权页面
	public void toOauth(){
		String calbackUrl=PropKit.get("domain")+"/oauth";
		String url=SnsAccessTokenApi.getAuthorizeURL(PropKit.get("appId"), calbackUrl, "111",false);
		redirect(url);
	}
	
	
	public void doCoupon(){
		String tburl = getPara("q");
        String sr=HttpRequest.sendPost("http://www.quanduoduo.com/index", "q="+ tburl);
        String retstr = RegexUtils.findResult("(uland.taobao.com[^\"]*)", sr);
		renderText(retstr);
	
	}
	
	
	public void my(){
		
		String openId = getSessionAttr("openId");
		WtWxuser u = WtWxuser.dao.findByOpenId(openId);
		setAttr("user",u);
		
		setAttr("active", "my");
		
		render("my.jsp");
	}
	
	public void shai(){
		
		String openId = getSessionAttr("openId");
		WtWxuser u = WtWxuser.dao.findByOpenId(openId);
		setAttr("user",u);
		
		String t = getPara("t");
		if ( t.equals("list")){
			render("shailist.jsp");	
		}else{
			render("shai.jsp");	
		}
	}

	
		
	public void list(){
		String conditions = "";
		String title = getPara("keywords");
		if ( title != null && ! title.isEmpty()){
			conditions = " and itemtitle like '%"+title+"%'";
		}
		int catid = getParaToInt("catid");
		WtCat cat = WtCat.dao.findById(catid);
		if ( cat != null && cat.getType().equals("sys")){
			
			conditions += " and catid =  " + catid;
		}
		
		int pageno = Integer.parseInt(getPara("limit"));
		//Page<TbkProduct> products = TbkProduct.dao.paginate(pageno,10,conditions);
		//setAttr("status",1); //标志成功
		Page<WtItem> products = WtItem.dao.paginate(pageno,10,conditions);
		setAttr("status",2); //标志成功		
		setAttr("content",products);
		renderJson();
	}
	
	public void item(){
		String id = getPara("id");
		
		
		
		WtItem p = WtItem.dao.findById(id);
		//TbkProduct p = TbkProduct.dao.findById(id);
		String taokoulin = p.getTaokoulin();
		if( taokoulin == null || p.getTaokoulin().isEmpty()){
			taokoulin = Duang.duang(TbkService.class).GenTaokoulin(id);
			p.setTaokoulin(taokoulin);
		}
		
		//int price2= p.getInt("price") - p.getInt("coupon_notes");
		setAttr("product",p);
		//setAttr("price2",price2);
		render("item.jsp");
	}

}
